package com.project.controller;

import com.project.bean.ShopBean;
import com.project.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Map;

@RestController
@RequestMapping("shop")
public class ShopController {
    @Autowired
    private IShopService service;

    @RequestMapping("buy")
    public Collection<ShopBean> buy(Integer productId, HttpServletRequest request){
        //从session中取出购物车
        Map<Integer,ShopBean> shopMap =
                (Map<Integer,ShopBean>)request.getSession().getAttribute("shopMap");
        if(productId != null){
            service.buy(productId, shopMap);
        }

        return shopMap.values();
    }

    @RequestMapping("remove")
    public String remove(Integer productId, HttpServletRequest request){
        //从session中取出购物车
        Map<Integer,ShopBean> shopMap =
                (Map<Integer,ShopBean>)request.getSession().getAttribute("shopMap");
        service.remove(productId, shopMap);

        return "ok";
    }

    @RequestMapping("updateNum")
    public String updateNum(Integer productId,int num, HttpServletRequest request){
        //从session中取出购物车
        Map<Integer,ShopBean> shopMap =
                (Map<Integer,ShopBean>)request.getSession().getAttribute("shopMap");
        service.updateNumber(productId, shopMap, num);

        return "ok";
    }
}
